﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyVoteMVC.Models;
using MyVoteMVC.Domain;
using MyVoteMVC.Extensions;
using MyVoteMVC.Service;
using MyVoteMVC.Common;
using System.Web.Security;
using System.IO;

namespace MyVoteMVC.Controllers
{
    public class ActivityController : Controller
    {
        //
        // GET: /Activity/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {
            IActivityService pService = Factory.GetInstance<IActivityService>();
            ActivityConverter pConverter = new ActivityConverter();
            var pList = pService.GetActivitiesList(DateTime.Now);
            var pResult = pList.ToModelList<VT_Activities, Activity>(pConverter);
            foreach (var pCR in pResult)
            {
                pCR.CurrentState = "活动进行中";
            }

            var pHistoryList = pService.GetHistoryActivitiesList(DateTime.Now);
            var pHistoryResult = pHistoryList.ToModelList<VT_Activities, Activity>(pConverter);
            foreach (var pHR in pHistoryResult)
            {
                pHR.CurrentState = "活动已结束";
                pResult.Add(pHR);
            }         
            return Json(pResult, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create(Activity pActivity)
        {
            FormsIdentity pIdentity = (FormsIdentity)HttpContext.User.Identity;
            FormsAuthenticationTicket pTicket = pIdentity.Ticket;

            IActivityService pService = Factory.GetInstance<IActivityService>();
            var pId = pService.AddActivity(string.Empty, pActivity.Title, pActivity.Content, pActivity.StartTime.Value, pActivity.EndTime.Value, 1, 1, 0, pTicket.Name);
            return Content("{success:true, msg:'" + pId + "'}");
        }

        public ActionResult Details(string id)
        {
            IActivityService pService = Factory.GetInstance<IActivityService>();
            ItemConverter pConverter = new ItemConverter();

            var pActivity = pService.GetActivities(id);
            var pQuestionList = pActivity.Questions;
            var pResult = new List<Item>();
            foreach (VT_Questions pQuestion in pQuestionList)
            {
                var pItemList = pQuestion.Items.ToModelList<VT_Items,Item>(pConverter);
                pResult.AddRange(pItemList);
            }
            return Json(pResult, JsonRequestBehavior.AllowGet);
        }

        //public ActionResult Save(string id, List<Item> Items)
        //{
        //    FormsIdentity pIdentity = (FormsIdentity)HttpContext.User.Identity;
        //    FormsAuthenticationTicket pTicket = pIdentity.Ticket;

        //    ITickService pService = Factory.GetInstance<ITickService>();
        //    ItemConverter pConverter = new ItemConverter();
        //    pService.CreateTickets(id, pTicket.UserData, Items, 1, pTicket.Name);
        //    return Content("{success:true}");
        //}

        public ActionResult Submit(string id, string Items)
        {
            FormsIdentity pIdentity = (FormsIdentity)HttpContext.User.Identity;
            FormsAuthenticationTicket pTicket = pIdentity.Ticket;

            ITickService pService = Factory.GetInstance<ITickService>();
            ItemConverter pConverter = new ItemConverter();
            //pService.SubmitTicket(id, pTicket.UserData, Items, pTicket.Name);
            return Content("{success:true}");
        }

        public ActionResult Thumbnail(string id)
        {
            //TODO:Add security checks
            IItemService pService = Factory.GetInstance<IItemService>();
            ItemConverter pConverter = new ItemConverter();

            var files = pService.GetItems(id);
            var image = files.ItemImage;
            while (image == null)
            {
                image = files.ItemImage;
            }

            if (image != null)
            {
                var desiredSize = new ImageSize { Width = 280, Height = 210 };

                string contentType = "image/png";

                var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());

                using (var stream = new MemoryStream(image.Item_Image))
                {
                    return File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType);
                }
            }

            throw new HttpException(404, "File Not Found");
        }

    }
}
